#include "JsonFile.h"

// Qt
#include <QJsonDocument>
#include <QCoreApplication>

bool readJsonFile(QIODevice & device, QSettings::SettingsMap & map)
{
	bool ret = false;

	QJsonParseError error;
	map = QJsonDocument::fromJson(device.readAll(), &error).toVariant().toMap();

	if (error.error == QJsonParseError::NoError)
		ret = true;

	return ret;
}

bool writeJsonFile(QIODevice & device, const QSettings::SettingsMap & map)
{
	bool ret = false;

	QJsonDocument jsonDocument = QJsonDocument::fromVariant(QVariant::fromValue(map));
	if (device.write(jsonDocument.toJson()) != -1)
		ret = true;

	return ret;
}

QString GetConfigPath()
{
	QString qexeFullPath = QCoreApplication::applicationDirPath();
	QString path = qexeFullPath + "/config.json";

	return path;
}

void InitSettings()
{
	const QSettings::Format JsonFormat = QSettings::registerFormat("json", readJsonFile, writeJsonFile);
	QSettings::setDefaultFormat(JsonFormat);
}

